package com.chebawang.controller;

import com.chebawang.repository.AccountRepository;
import com.chebawang.entity.Account;
import com.chebawang.entity.Result;
import com.chebawang.util.ResultUtil;
import org.apache.commons.codec.digest.DigestUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(description = "账号管理")
@RestController
@RequestMapping("account")
public class AccountController {


    @Autowired
    private AccountRepository accountRepository;


    @ApiOperation(value = "账号列表", notes = "账号列表")
    @GetMapping("/list")
    public Result atList(@RequestParam Integer page, @RequestParam Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<Account> atPage = accountRepository.findAll(pageable);
        return ResultUtil.success(atPage);
    }


    @ApiOperation(value = "账号登录", notes = "账号登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"num\": \"账号\",\"password\": \"密码\"}", required = true, dataType = "String"),
    })
    @PostMapping("/login")
    public Result findByNum(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String password = tempData.getString("password");
        String num = tempData.getString("num");
        Account a = accountRepository.findAllByNumAndPassword(num, password);
        if (a == null) {
            return ResultUtil.error(-1, "账号密码有误！");

        }
        return ResultUtil.success(a);
    }


    @ApiOperation(value = "添加账号", notes = "添加账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"num\": \"账号\",\"password\": \"密码\",\"name\": \"姓名\",\"aType\": \"账号类型 0管理员  1维修员   2查看员\"}", required = true, dataType = "String"),
    })
    @PostMapping("/save")
    public Result save(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String password = tempData.getString("password");
        String num = tempData.getString("num");
        String name = tempData.optString("name");
        int aType = tempData.getInt("aType");
        Account a = new Account();
        a.setCreateDate(new Date());
        a.setMd5pass(DigestUtils.md5Hex(password));
        a.setName(name);
        a.setNum(num);
        a.setPassword(password);
        a.setaType(aType);
        return ResultUtil.success(accountRepository.save(a));
    }

    @ApiOperation(value = "修改账号", notes = "修改账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\": \"账号id\",\"name\": \"姓名\",\"aType\": \"账号类型 0管理员  1维修员   2查看员\"}", required = true, dataType = "String"),
    })
    @PostMapping("/edit")
    public Result edit(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String id = tempData.getString("id");
        String name = tempData.optString("name");
        int aType = tempData.getInt("aType");
        Account a = accountRepository.findOne(id);
        a.setName(name);
        a.setaType(aType);
        return ResultUtil.success(accountRepository.save(a));
    }



    @ApiOperation(value = "删除账号", notes = "删除账号")
    @GetMapping("/del")
    public Result del(@RequestParam String id) {
        accountRepository.delete(id);
        return ResultUtil.success();
    }


}

